package com.moneywelcome.riskassembly.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.moneywelcome.dto.Activations;
import com.moneywelcome.dto.HitsDetail;
import com.moneywelcome.dto.RiskResult;
import com.moneywelcome.exceptions.MwcArgsException;
import com.moneywelcome.results.StatusCode;

public class RiskResultUtil {

    public static RiskResult parseResult(String result) {
        if(StrUtil.contains(result,"异常")) throw new MwcArgsException(result, StatusCode.Error);
        if(result.length()<200) throw new MwcArgsException(result, StatusCode.Error);
        String json1 = result.substring(result.indexOf("{", result.indexOf("activations") + 20), result.indexOf("}", result.indexOf("activations")) + 1);
        String json2="{}";
        try {
            json2 = result.substring(result.indexOf("{", result.indexOf("hitsDetail") + 20), result.indexOf("}", result.indexOf("hitsDetail")) + 1);
        } catch (StringIndexOutOfBoundsException e) {
        }
            RiskResult riskResult = new RiskResult();
            riskResult.setActivations(JSONUtil.toBean(json1, Activations.class));
            riskResult.setHitsDetail(JSONUtil.toBean(json2, HitsDetail.class));
            return riskResult;
        }
    }
